package com.tywj.buscustomerapp.view.activity;

import android.graphics.Bitmap;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.view.myview.LoadingDialog;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;

public class InfoActivity extends BaseActivity {
    @BindView(R.id.webview)
    WebView webView;
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.head_left)
    ImageView back;

    private LoadingDialog waitDialog;
    private static String[] urls = {"http://114.115.175.229:8085/用户协议.html",
        "http://114.115.175.229:8085/购票须知.html"};
    private static String[] titles = {"用户协议",
            "购票须知"};

    @Override
    protected int getContentId() {
        return R.layout.activity_info;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        WebSettings settings = webView.getSettings();
        settings.setTextZoom(100);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBlockNetworkImage(false);
        int sdk = Build.VERSION.SDK_INT;
        if(sdk >= Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                closeLoading();
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        if (getIntent()!=null){
            String url = urls[getIntent().getIntExtra("type",0)];
            title.setText(titles[getIntent().getIntExtra("type",0)]);
            showLoading();
            webView.loadUrl(url);
        }

    }

    public void showLoading() {
        if (waitDialog==null)
            waitDialog = new LoadingDialog(InfoActivity.this);
        if (!waitDialog.isShowing())
            waitDialog.show();
    }

    public void closeLoading() {
        try {
            if (waitDialog != null && waitDialog.isShowing())
                waitDialog.dismiss();
        }catch (IllegalArgumentException e){
            waitDialog = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("协议");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("协议");
        MobclickAgent.onPause(this);
    }

}
